/*
 *   Copyright (c) 2010 Sonrisa Informatikai Kft. All Rights Reserved.
 * 
 *  This software is the confidential and proprietary information of
 *  Sonrisa Informatikai Kft. ("Confidential Information").
 *  You shall not disclose such Confidential Information and shall use it only in
 *  accordance with the terms of the license agreement you entered into
 *  with Sonrisa.
 * 
 *  SONRISA MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
 *  THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
 *  TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
 *  PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SONRISA SHALL NOT BE LIABLE FOR
 *  ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
 *  DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
 */

package com.zh.tree;

import java.util.Enumeration;
import javax.swing.tree.TreeNode;

/**
 *
 * @author Golyo
 */
public class LinkedTreeNode<T> implements TreeNode {
    private boolean allowsChildrer;
    private LinkedTreeNode<T> next;
    private LinkedTreeNode<T> parent;
    private LinkedTreeNode<T> firstChild;


    @Override
    public Enumeration<LinkedTreeNode<T>> children() {
        return new LinkedTreeChildrenEnumeration(firstChild);
    }

    @Override
    public boolean getAllowsChildren() {
        return allowsChildrer;
    }

    @Override
    public LinkedTreeNode getChildAt(int childIndex) {
        LinkedTreeNode<T> actual = firstChild;
        int idx = 0;
        while (idx < childIndex && actual != null) {
            actual = actual.next;
        }
        return actual;
    }

    @Override
    public int getChildCount() {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public int getIndex(TreeNode node) {
        LinkedTreeNode<T> actual = firstChild;
        int idx = 0;
        while (actual != null && !actual.equals(node)) {
            actual = actual.next;
            idx++;
        }
        return actual != null ? idx : -1;
    }

    @Override
    public LinkedTreeNode getParent() {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public boolean isLeaf() {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public class LinkedTreeChildrenEnumeration implements Enumeration<LinkedTreeNode<T>> {
        private LinkedTreeNode<T> actual;
        protected LinkedTreeChildrenEnumeration(LinkedTreeNode<T> actual) {
            this.actual = actual;
        }
        @Override
        public boolean hasMoreElements() {
            return  actual.next != null;
        }

        @Override
        public LinkedTreeNode<T> nextElement() {
            actual = actual.next;
            return actual;
        }

    }
}
